package com.peanut.baby.mvp.bind;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.peanut.baby.AppConfig;
import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.comm.BaseDialog;
import com.peanut.baby.http.ApiException;
import com.peanut.baby.mvp.BaseMvpActivity;
import com.peanut.baby.mvp.login.LoginActivity;
import com.peanut.baby.mvp.main.MainActivity;
import com.peanut.devlibrary.imageloader.ImageLoader;
import com.peanut.devlibrary.util.StringUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by anonymouslystudio on 2017/7/11.
 */

public class BindMobileActivity extends BaseMvpActivity implements BindContract.View,
        View.OnClickListener {


    @BindView(R.id.phone_nubmber_edit)
    EditText mobileEdit;
    @BindView(R.id.identifying_code_edit)
    EditText codeEdit;
    @BindView(R.id.get_vcode)
    TextView getVcode;
    @BindView(R.id.bind_submit)
    TextView bindSubmit;
    @BindView(R.id.bind_avatar)
    CircleImageView bindAvatar;
    @BindView(R.id.bind_nick)
    TextView bindNick;
    private BindMobilePresenter presenter;

    private boolean requestResult = false;

    /**
     * will not return login result
     *
     * @param ctx
     */
    public static void start(Context ctx) {
        Intent intent = new Intent(ctx, BindMobileActivity.class);
        ctx.startActivity(intent);
    }

    /**
     * will return login result
     *
     * @param ctx
     */
    public static void startForResult(Activity ctx) {
        Intent intent = new Intent(ctx, BindMobileActivity.class);
        intent.putExtra("requestResult", true);
        ctx.startActivityForResult(intent, AppConfig.RequestResult.REQUEST_CODE_BIND);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind);
        ButterKnife.bind(this);
        requestResult = getIntent().getBooleanExtra("requestResult",false);
        presenter = new BindMobilePresenter(this,this);
        initViews();
        bindDefaultData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestory();
    }

    private void initViews() {
        getVcode.setOnClickListener(this);
        bindSubmit.setOnClickListener(this);

    }

    private void bindDefaultData(){
        ImageLoader.getInstance().loadImageView(this, InitManager.getInstance().getUser().avatar,bindAvatar);
        bindNick.setText(InitManager.getInstance().getUser().nickname);
    }


    @Override
    public void onBindSuccess() {
        dismissProgressDialog();
        if(requestResult){
            setResult(RESULT_OK);
            finish();
        }else{
            MainActivity.start(BindMobileActivity.this);
            finish();
        }
    }

    @Override
    public void onBindFailed(String code, final String failReason) {
        dismissProgressDialog();
        if(code.equals(ApiException.ERROR_BIND_CONFLIC)){
            new BaseDialog().showConfirmCancelAlert(BindMobileActivity.this, failReason, "登录", "取消", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    InitManager.getInstance().setUser(null);
                    LoginActivity.startAndClear(BindMobileActivity.this);
                    finish();
                }
            },null);
        }else{
            showToast("绑定失败 "+failReason);
            getVcode.setEnabled(true);
            getVcode.setText("获取验证码");
            mobileEdit.setEnabled(true);
        }
    }

    @Override
    public void startCountDown() {
        dismissProgressDialog();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getVcode.setEnabled(false);
                mobileEdit.setEnabled(false);
            }
        });
    }

    @Override
    public void onCountDown(final int current) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getVcode.setText(current + "");
            }
        });
    }

    @Override
    public void onCountComplete() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getVcode.setEnabled(true);
                getVcode.setText("重新获取验证码");
                mobileEdit.setEnabled(true);
            }
        });
    }

    @Override
    public void onRequestSMSCodeFailed(final String reason) {
        dismissProgressDialog();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(reason);
                mobileEdit.setEnabled(true);
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.get_vcode:
                queryVcode();
                break;
            case R.id.bind_submit:
                submitBindRequest();
                break;

        }
    }


    /**
     * 获取验证码
     */
    private void queryVcode() {
        String mobile = mobileEdit.getText().toString();
        if (StringUtil.isNullOrEmpty(mobile) || !StringUtil.isMobile(mobile)) {
            showToast("请输入手机号");
            return;
        }
        showProgressDialog("请稍候...",false);
        mobileEdit.setEnabled(false);
        presenter.getVCode(mobile);
    }

    /**
     *
     */
    private void submitBindRequest() {
        String mobile = mobileEdit.getText().toString();
        String code = codeEdit.getText().toString();
        if (StringUtil.isNullOrEmpty(mobile) || !StringUtil.isMobile(mobile)) {
            showToast("请输入手机号");
            return;
        }
        if (StringUtil.isNullOrEmpty(code)) {
            showToast("请输接收到的验证码");
            return;
        }
        hideSoftKeyboard();
        presenter.submitBind(mobile, code);
    }
}
